import { getmenu } from "../mysql/index.js";
import { Request, Response, NextFunction } from 'express';
import config from "../config/default.js";
import { formatterMenuTree } from "../toolClass/listToTree.js";

export default (req: Request, res: Response, next: NextFunction) => {
  const queryParams = req.body;
  const myCookie = req.signedCookies?.token;
  if (!myCookie || myCookie !== config.cookie) {
    res.status(405).json(JSON.stringify({
      code: "405",
      message: "登录过期！"
    }))
    return
  }
  getmenu(queryParams.username, queryParams.password)
    .then((data) => {
      res.status(200);
      res.send(JSON.stringify({
        code: "200",
        data: formatterMenuTree(data as [], "menuFolderId", "menuParentFolderId")
      }))
    }).catch((e) => {
      console.warn(e);
      res.status(500).json(JSON.stringify({
        code: "500",
        message: "数据库查询出错！"
      }))
    })
}